<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="节点名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nodeName">
              <a-input :disabled="formDisabled" v-model="model.nodeName" placeholder="请输入节点名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="节点进度"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="nodeProgress"
              style="width:100%">
              <a-input-number
                :disabled="formDisabled"
                style="width:100%"
                v-model="model.nodeProgress"
                placeholder="请输入节点进度"
                :precision="2"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="证明材料是否必填" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isValidateRequired">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="radio"
                v-model="model.isValidateRequired"
                dictCode="YES_OR_NO"
                placeholder="证明材料是否必填" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { postAction } from '@/api/manage';
import { mapGetters } from 'vuex';
export default {
  name: 'ErpProgressTaskNodeChildNodeForm',
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        isValidateRequired: '1'
      },
      nodeModel: {},
      labelCol: {
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        nodeName: [{ required: true, message: '进度请输入节点名称' }],
        isValidateRequired: [{ required: true, message: '请选择证明材料是否必填' }],
        nodeProgress: [{ required: true, message: '请输入节点进度' },
          { pattern: /^[0-9]+.?[0-9]*$/, message: '节点进度请输入正确数字' }
        ]
      },
      url: {
        add: '/api/erp/progress/erpProgressTemplateDt/add',
        edit: '/api/erp/progress/erpProgressTemplateDt/edit',
        queryById: '/api/erp/progress/erpProgressTemplateDt/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    ...mapGetters(['userInfo']),
    add(nodeModel) {
      this.edit(this.modelDefault, -1);
      this.$nextTick(() => {
        this.nodeModel = nodeModel
      })
    },
    edit(record) {
      // this.$refs.form.resetFields();
      Object.assign(this.$data, this.$options.data())
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      var that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          if (!this.model.id) {
            this.model.dataId = parseInt(Math.random() * 1000000000000);
            this.model.id = '';
          }
          let params = {
            handlerId: that.userInfo().empId, // 当前登录人Id
            parentNodeId: that.nodeModel.id, // 父节点id
            progressTaskId: that.nodeModel.progressTaskId, // 	任务进度Id
            taskId: that.nodeModel.taskId, // 任务Id
            nodeName: that.model.nodeName, // 节点名称
            nodeProgress: that.model.nodeProgress, // 节点名称
            isValidateRequired: that.model.isValidateRequired, // 证明材料是否必填
            isChild: 1 // 必传
          }
          // 判断节点进度不得小于等于上一相邻节点，不得大于等于下一相邻节点
          postAction('/api/erp/progress/erpProgressTaskNode/addVerify', params).then(res => {
            if (res.success) {
              that.$emit('ok', this.model);
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    }
  }
}
</script>
